package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupServcie;
import com.itheima.utils.MethodCountUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = CheckGroupServcie.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupServcie {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //操作新增检查组 t_checkgroup 表
        checkGroupDao.add(checkGroup);

        // 检查组添加接口调查次数统计
        MethodCountUtils.methodCount("CheckGroupAdd");

        //设置检查组和检查项的多对多关联关系,操作t_checkgroup_车此贴em表
        Integer checkGroupId = checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkitemIds, checkGroupId);

    }



    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);


        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);
        // 检查组查询 接口调查次数统计
        MethodCountUtils.methodCount("CheckGroupPageQuery");
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(Integer[] checkitemIds,CheckGroup checkGroup) {
        //修改检查主基本信息,操作检查组t_checkgroup表
        checkGroupDao.edit(checkGroup);
        //清理房钱检查组关联的检查项,操作中间关系表t_checkgroup_checkitem表
        checkGroupDao.deleteAssocication(checkGroup.getId());

        // 检查组查询 接口调查次数统计
        MethodCountUtils.methodCount("CheckGroupEdit");
        //重新建立当前检查组的检查项关联关系
        Integer checkGroupId = checkGroup.getId();
        setCheckGroupAndCheckItem(checkitemIds, checkGroupId);

    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    @Override
    public void deleteById(Integer id) {
        long count = checkGroupDao.findCountCheckGroupId(id);

        if (count > 0) {
            throw new RuntimeException("当前检查组被引用，不能删除");
        }
        checkGroupDao.deleteAssocication(id);
        checkGroupDao.deleteById(id);

        // 检查组查询 接口调查次数统计
        MethodCountUtils.methodCount("CheckGroupDelete");
    }


    private void setCheckGroupAndCheckItem(Integer[] checkitemIds, Integer checkGroupId) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroupId);
                map.put("chcekitem_id",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
